<?php

declare(strict_types=1);
namespace Service\PointsService;

use Service\BaseService;
use App\Model\BbcModel\PointsLog;
use App\Model\BbcModel\Member;
use App\Model\BbcModel\Order;
use App\Model\BbcModel\OrderExtends;
use Service\Code\Normal;
use App\Model\BbcModel\PointsConfig;
use App\Model\BbcModel\GoodsPointsConfig;

/**
 * 积分服务
 * @author chenshubo
 */
class PointsService extends BaseService
{
    /**
     * 写入用户积分日志
     * @param int $cid
     * @param int $mid
     * @param int $mn_sid
     * @param int $points
     * @param int $type
     * @param string $remark
     */
    public function addUserPointsChangLog(int $cid,int $mid,int $mn_sid, $points,int $type,string $remark)
    {
        $points_data = [
            'cid'       => $cid,
            'mid'       => $mid,
            'type'      => $type,
            'points'    => $points,
            'mn_sid'    => $mn_sid,
            'cd'        => time(),
            'remark'    => $remark
        ];
        return PointsLog::instance()->add($points_data);
    }
    
    /**
     * 操作用户积分逻辑
     * @param int $cid
     * @param int $mid
     * @param int $mn_sid
     * @param int $points
     * @param array $extra
     */
    public function deductUserPoints(int $cid,int $mid,int $mn_sid, $points,array $extra = [] )
    {
        $memberpointsres = Member::instance()->where(['mid'=>$mid])->decrement('points',$points);
        if(!$memberpointsres){
            return Normal::returnCode(Normal::FAIL, '储值余额扣除失败');
        }
        
        //写入积分变动日志表
        $pointslog = $this->addUserPointsChangLog($cid,$mid, $mn_sid, $points,0,'线下购物消费');
        if(!$pointslog){
            return Normal::returnCode(Normal::FAIL, '储值余额变动写入失败');
        }
        
        //处理是否满足积分赠送的条件
        $pointsconfig_give = PointsConfig::instance()->info(
            [
                ['money','<=',$points],
                ['mn_sid','=',$mn_sid],
                ['type','=',2],
                '_order_' => [
                    'money' => 'desc'
                ]
            ]);
        
        if($pointsconfig_give && $pointsconfig_give['points'] > 0 )  
        {
            if( empty($extra['order_id']) ) {
                return Normal::returnCode(Normal::FAIL, '传入订单order_id不能为空');
            }
            
            if( empty($extra['order_extends_id']) ) {
                return Normal::returnCode(Normal::FAIL, '传入订单order_extends_id不能为空');
            }
            
            $orderedit= Order::instance()->edit(['id'=>$extra['order_id']], ['give_points'=>$pointsconfig_give['points']]);
            if(!$orderedit){
                return Normal::returnCode(Normal::FAIL, '订单赠送余额更新失败');
            }
            
            $orderextendsedit= OrderExtends::instance()->edit(['id'=>$extra['order_extends_id']], ['give_points'=>$pointsconfig_give['points']]);
            if(!$orderextendsedit){
                return Normal::returnCode(Normal::FAIL, '订单扩展赠送余额更新失败');
            }
            
            $memberpointsgiveres= Member::instance()->where(['mid'=>$mid])->increment('points',$pointsconfig_give['points']);
            if(!$memberpointsgiveres){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败1');
            }
            
            $memberpointsgiveres2= Member::instance()->where(['mid'=>$mid])->increment('points_history',$pointsconfig_give['points']);
            if(!$memberpointsgiveres2){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败11');
            }
            
            $pointsloggive = $this->addUserPointsChangLog($cid,$mid, $mn_sid, $pointsconfig_give['points'],1,"消费余额赠送{$pointsconfig_give['points']}储值余额");
            if(!$pointsloggive){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败2');
            }
        }
        return Normal::returnCode(Normal::SUC, '处理完成');
    }
    
    /**
     * 获取积分抵扣配置
     * @param int $cid
     */
    public function getChannelPointsConfig(int $cid)
    {
        $goodsPointsInfo = GoodsPointsConfig::instance()->info(['cid'=>$cid]);
        return [
            'points_auto'           => $goodsPointsInfo['points_auto'] ?? 0,
            'return_points_auto'    => $goodsPointsInfo['return_points_auto'] ?? 0
        ];
    }
    
    /*
     * 订单退款,返还积分
     */
    public function returnPointsByReturnOrder(int $cid,int $mid,int $mn_sid, $points,array $extra = []) {
        $memberpointsres = Member::instance()->where(['mid'=>$mid])->increment('points',$points);
        if(!$memberpointsres){
            return Normal::returnCode(Normal::FAIL, '储值余额返还失败');
        }
        //写入积分变动日志表
        $pointslog = $this->addUserPointsChangLog($cid,$mid, $mn_sid, $points,1,'线下退货退款');
        if(!$pointslog){
            return Normal::returnCode(Normal::FAIL, '储值余额分变动写入失败');
        }
        if($extra['give_points']>0){
            $memberpointsgiveres= Member::instance()->where(['mid'=>$mid])->decrement('points',$extra['give_points']);
            if(!$memberpointsgiveres){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败1');
            }
            
            $memberpointsgiveres2= Member::instance()->where(['mid'=>$mid])->decrement('points_history',$extra['give_points']);
            if(!$memberpointsgiveres2){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败11');
            }
            
            $pointsloggive = $this->addUserPointsChangLog($cid,$mid, $mn_sid, $extra['give_points'],0,"线下退货退赠送{$extra['give_points']}储值积分");
            if(!$pointsloggive){
                return Normal::returnCode(Normal::FAIL, '储值余额赠送失败2');
            }
        }
        return Normal::returnCode(Normal::SUC, '处理完成');
    }
}
